using AutoMapper;
using StudentManagement.Application.DTOs;
using StudentManagement.Domain.Entities;
using StudentManagement.Domain.Repositories;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace StudentManagement.Application.Services
{
    public class ClassService : IClassService
    {
        private readonly IClassRepository _classRepository;
        private readonly IStudentRepository _studentRepository;
        private readonly IMapper _mapper;

        public ClassService(IClassRepository classRepository, IStudentRepository studentRepository, IMapper mapper)
        {
            _classRepository = classRepository;
            _studentRepository = studentRepository;
            _mapper = mapper;
        }

        public async Task<ClassDTO> GetClassByIdAsync(int id)
        {
            var @class = await _classRepository.GetByIdAsync(id);
            if (@class == null)
            {
                return null;
            }

            var classDTO = _mapper.Map<ClassDTO>(@class);
            return classDTO;
        }

        public async Task<IEnumerable<ClassDTO>> GetAllClassesAsync()
        {
            var classes = await _classRepository.GetAllAsync();
            return _mapper.Map<IEnumerable<ClassDTO>>(classes);
        }

        public async Task<IEnumerable<ClassDTO>> GetClassesByGradeAsync(string grade)
        {
            var classes = await _classRepository.GetClassesByGradeAsync(grade);
            return _mapper.Map<IEnumerable<ClassDTO>>(classes);
        }

        public async Task<IEnumerable<ClassDTO>> GetClassesByMajorAsync(string major)
        {
            var classes = await _classRepository.GetClassesByMajorAsync(major);
            return _mapper.Map<IEnumerable<ClassDTO>>(classes);
        }

        public async Task<ClassDTO> CreateClassAsync(ClassDTO classDTO)
        {
            var @class = _mapper.Map<Class>(classDTO);
            await _classRepository.AddAsync(@class);

            return _mapper.Map<ClassDTO>(@class);
        }

        public async Task<ClassDTO> UpdateClassAsync(int id, ClassDTO classDTO)
        {
            var @class = await _classRepository.GetByIdAsync(id);
            if (@class == null)
            {
                throw new Exception("班级不存在");
            }

            _mapper.Map(classDTO, @class);
            await _classRepository.UpdateAsync(@class);

            return _mapper.Map<ClassDTO>(@class);
        }

        public async Task DeleteClassAsync(int id)
        {
            var @class = await _classRepository.GetByIdAsync(id);
            if (@class == null)
            {
                throw new System.Exception("班级不存在");
            }

            // 检查班级是否有学生
            var students = await _studentRepository.GetStudentsByClassIdAsync(id);
            if (students.Any())
            {
                throw new System.Exception("班级有学生，无法删除");
            }

            await _classRepository.DeleteAsync(id);
        }

        public async Task<bool> ClassExistsAsync(int id)
        {
            return await _classRepository.GetByIdAsync(id) != null;
        }

        public async Task<IEnumerable<ClassDTO>> SearchClassesAsync(string keyword)
        {
            var classes = await _classRepository.SearchClassesAsync(keyword);
            return _mapper.Map<IEnumerable<ClassDTO>>(classes);
        }

        public async Task<ClassDTO> GetClassWithStudentsAsync(int id)
        {
            // 先获取所有班级及其学生
            var allClassesWithStudents = await _classRepository.GetClassesWithStudentsAsync();
            
            // 找到指定id的班级
            var @class = allClassesWithStudents.FirstOrDefault(c => c.Id == id);
            if (@class == null)
            {
                throw new Exception("班级不存在");
            }

            var classDTO = _mapper.Map<ClassDTO>(@class);
            // 可以在这里添加学生数量或其他相关信息
            return classDTO;
        }
    }
}